import util from "../../utils/util.js"
// component/teacher-list/teacher-list.js
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        teacherList: {
            type: Array
        },
        showAd: {
            type: Boolean
        }
        /*例如
        teacherList: [{
            access_number: 1,
            address: "小谷围街道",
            city: "广州市",
            contact_name: (2)["王老师", "李老师"],
            contact_phone: (2)["13790933699", "14755896631"],
            district: "番禺区",
            district: "番禺区",
            email: "12465660@qq.com",
            grade: "高中",
            has_organized: "编制内",
            is_organize: true,
            original_web: "https://www.baidu.com",
            other_info: "无",
            post_level: "高中",
            province: "广东省",
            registration_begin: 1625500800000,
            registration_end: 1633536000000,
            registration_way: "网络报名",
            release_time: 1635255513388,
            school: "育才学校",
            school_name: "龙华中学",
            search_string: "广东省广州市番禺区小谷围街道龙华中学语文",
            stage: "结束报名",
            subject: "语文老师",
            _id: "859059a56177d8760231a7c00b5afbfa",
            _updateTime: 1637461631747
        }]
        */

    },

    /**
     * 组件的初始数据
     */
    data: {
        my_release_time: null //my_release_time:Array,一个“计算属性”，当teacherList被传入时设置
    },

    /**
     * 组件的方法列表
     */
    methods: {

        async JumpToDetail(e) {
            const that = this
            //先判断是否登录
            if (getApp().globalData.isLogin == undefined) {
                wx.$cloud.callFunction({
                    name: "inviteUser",
                    data: {
                        sign: 5
                    },
                    success: res => {
                        //如果该用户未登陆过
                        if (res.result.length == 0) {
                            wx.getStorage({
                                key: 'look_limit',
                                success: function (res) {
                                    // 异步接口在success回调才能拿到返回值
                                    console.log("成功获取到数据")
                                    console.log("数据：", res.data)
                                    that.setData({
                                        look_limit: res.data
                                    })
                                    if (that.data.look_limit >= 3) {
                                        wx.showToast({
                                            title: '请先登录',
                                            icon: "error",
                                            duration: 1000
                                        })
                                        setTimeout(function () {
                                            wx.navigateTo({
                                                url: "/pages/login_main/login_main",
                                                success: (result) => {

                                                },
                                                fail: () => {},
                                                complete: () => {}
                                            });
                                        }, 1000)
                                    } else {
                                        that.setData({
                                            look_limit: that.data.look_limit + 1
                                        })
                                        wx.setStorage({
                                            key: "look_limit",
                                            data: that.data.look_limit,
                                        })
                                        const props = e.currentTarget.dataset.item
                                        console.log("hhhhh", props)
                                        wx.navigateTo({
                                            url: '/pages/detail/detail',
                                            events: {
                                                //仅用于传递参数
                                                propsFunc() {}
                                            },
                                            success: (res) => {
                                                // 通过eventChannel向被打开页面传送数据
                                                res.eventChannel.emit('propsFunc', props)
                                                //浏览数+1
                                                wx.$cloud.callFunction({
                                                    name: 'return-recruit-info',
                                                    data: {
                                                        action: 'detail',
                                                        id: e.currentTarget.dataset.item._id
                                                    },
                                                    // 成功回调
                                                    complete: console.log
                                                })
                                            },
                                            fail: () => {},
                                            complete: () => {}
                                        });
                                    }
                                },

                                fail: function () {
                                    console.log('未创建该变量')
                                    wx.setStorage({
                                        key: "look_limit",
                                        data: 0,
                                    })

                                }
                            })
                            /*
                            console.log("res:",result)
                            console.log("*******************:",that.data.look_limit)
                            console.log("**************data:",that.data)
                            */
                        } else {
                            getApp().globalData.isLogin = true
                            this.conclVip(e)
                        }
                    }
                })
            } else {
                this.conclVip(e)
            }
        },
        async conclVip(e) {
            //判断岗位限制
            // let temp=await wx.cloud.callFunction({
            //     name: "vipLimit",
            //     data: {
            //         sign: 0,
            //     }
            // })
            // temp=temp.result
            // if(temp.errCode==1){
            //     wx.showToast({
            //       title: '查看次数已用完!',
            //       icon: 'error',
            //       duration: 2000,
            //       success:res =>{
            //         setTimeout(function(){
            //             wx.navigateTo({
            //                 url: '../../pages/huiyuan/huiyuan',
            //               })
            //         },2000)
            //       }
            //     })
            //     return ;
            // }
            const props = e.currentTarget.dataset.item
            console.log("hhhhh", props)
            wx.navigateTo({
                url: '/pages/detail/detail',
                events: {
                    //仅用于传递参数
                    propsFunc() {}
                },
                success: (res) => {
                    // 通过eventChannel向被打开页面传送数据
                    res.eventChannel.emit('propsFunc', props)
                    //浏览数+1
                    wx.$cloud.callFunction({
                        name: 'return-recruit-info',
                        data: {
                            action: 'detail',
                            id: e.currentTarget.dataset.item._id
                        },
                        // 成功回调
                        complete: console.log
                    })
                },
                fail: () => {},
                complete: () => {}
            });
        }
    },
    observers: {
        'teacherList': function (teacherList) { //监听teacherList，以计算格式化后的时间
            // console.log('teacherList被设置啦', teacherList)
            const my_release_time = []
            for (const item of teacherList) {
                const time_a = util.formatTime(new Date(item.release_time)) //此时得到形如2021-11-23
                // console.log(time_a)
                const year = time_a.substr(0, 4)
                let month = time_a.substr(5, 2)
                let day = time_a.substr(8, 2)
                //去掉前导0
                if (month[0] == '0') {
                    month = month.substring(1)
                }
                if (day[0] == '0') {
                    day = day.substring(1)
                }
                const time_b = year + '年' + month + "月" + day + '日' //此时得到形如2021-11-23
                // console.log(time_b)
                my_release_time.push(time_b)
            }
            this.setData({
                my_release_time: my_release_time
            })
            // console.log('this.data.my_release_time', this.data.my_release_time)
        }
    }
})